/*******************************************************************************
* Homework 3: Trip.cpp, CSCE 240, Spring 2010
* Author: Andrew Shore
* Date Last Modified: 17 Feburary 2010
*
* Decription: This class consists of a constructor, destructor, and a single 
* function called balanceExpenses. The balanceExpenses function reads an array
* of money each student pays calculates the total cost of the trip, how much 
* money needs to be exchanged to balance the expenses, and how much each 
* student needs to pay or be reimbursed to balance the expenses. This
* information is formatted and displayed. Multiple trips can be processed but
* this is handled in the main function. Program includes header file 'Trip.h'
 ******************************************************************************/
#include "Trip.h"


  
// constructor and destructor
Trip::Trip(){
}

Trip::~Trip(){
}
  
/*******************************************************************************
 * Function to display the results of balancing the trip's expenses in
 *  standard output.
 * Author: Andrew Shore
 * Date Last Modified: 17 February 2010
 * 
 * First, a loop is run to calcuate the total cost of the trip based on
 * on much each student paid. Then the average is calculated by dividing
 * the sum by the total number of students.  A second loop is run to find
 * the total amount required to change hands by taking the 'absolute value'
 * of the sum of the difference of the amount each student paid and the
 * average divided by 2. This information is the displayed and a third for
 * loop prints how much each student needs to pay or be reimbursed to
 * balance the expenses.
 *
 * No error checking is done for the numberOfStudents. We assume that is
 * done in the main function. However, there is error checking to ensure
 * the inputs for how much each student paid is non-negative. We also assume
 * that student[] is non-empty
 *
 * Parameters:
 * double students[] - array containing the data on how much each student paid
 * int numberOfStudents- how many students went on the trip
 *
 * Ouput: none expect for formatted display information sent to standard 
 *        output
 *
 *
 * Returns: none
 */
void Trip::balanceExpenses(double students[], int numberOfStudents){
   //enumerated constants for formatted printing
   enum {PRINTWIDTH = 5};
   enum {PRINTWIDTHTWO = 10};
   enum {PRINTPRECISION = 2};

   //enumerated constant for calculating amountChangingHands
   enum {TWO = 2};

   /*Variables:
     cost - total cost of trip
     difference - used to find the total deviation from the average
     average - amount each student needs to pay to balance expenses
     amountChangingHands - total amount of money needed to be exchanged 
                            to balance expenses
     message - string for printing out 'to be paid' and to be reimbursed based
                on what the student paid for the trip
   **/
   double cost = 0;
   double difference = 0;
   double average;
   double amountChangingHands;
   string message;

   //for loop asks user to input how much each student paid while totaling
   //the amount
   for(int i = 0 ; i < numberOfStudents ; i++){
     
     cout << "Please enter the amount that student " << (i + 1) << " paid: \n";
      cin >> students[i];

      //makes sure input is non-negative
      if(students[i] < 0){
         cout << "ERROR: illegal negative value.";
         exit(0);
      }
   
      cost += students[i];
    }
  
  
   average =  cost / static_cast<double>(numberOfStudents);
   
    
   //for loop to calculate the amount changing hands
   for(int i = 0; i < numberOfStudents ; i++){
      
      //if student paid more than the average difference is reversed
      if( average < students[i] ){
     
         //adds to the distance from the average
         difference += students[i] - average;
	
      }else{
  
         difference += average - students[i];
        
	
       }
   }
   /* total deviation from the average divided by two, it is 
      divided by two because for example 5 dollars to be reimbursed
      and 5 dollars to be paid doesn't mean 10 dollars change hands,
      one students gives the other 5 dollars and it is balanced
   **/
   amountChangingHands = difference / static_cast<double>(TWO) ;
   

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(PRINTPRECISION);
   cout << "There were " << numberOfStudents << " students and a total"
        << " of $" << cost << " spent.\n";
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(PRINTPRECISION);
   cout << "The minimum amount changing hands to balance accounts is"
        << " $" << amountChangingHands << endl;
   cout << "The amounts to be adjusted are: \n";

   //for loop that displays how much each students owes or is owed
   for(int i = 0; i < numberOfStudents; i++){

      //if student paid more than the average he needs to be reimbursed, 
      //else he has to pay another student to balance the expenses
      if(average <  students[i] ){

         /*difference in this case is not summed together as in the previous
           for loop, instead it is calculated seperately for each student to
           show how much they need to pay or be reimbursed
	 **/
         difference = students[i] - average;
	 message = " to be reimbursed.";

      }else{
  
         difference = average - students[i];
         message = " to be paid.";
	
       }

      cout.setf(ios::right);
      cout.width(PRINTWIDTH);
      cout << "Student";
      cout.setf(ios::right);
      cout.width(PRINTWIDTH);
      //i+1 index because we start at 0
      cout << (i + 1) << ":";
      cout.setf(ios::showpoint);
      cout.precision(PRINTPRECISION);
      cout.setf(ios::right);
      cout.width(PRINTWIDTHTWO);
      cout << difference << message << endl;
   
   }
}// void Trip::balanceExpenses(double students[], int numberOfStudents)
    

  
